package net.telesing.tcmc.back.logic;

import net.telesing.tcmc.back.dao.user.impl.UserDaoImpl;
import net.telesing.tcmc.back.model.MenuModel;
import net.telesing.tcmc.back.model.UserModel;
import net.telesing.tcmc.common.LogicException;
import net.telesing.tcmc.core.CryptUtil;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import sun.awt.geom.AreaOp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 一凡 on 2015/11/3.
 */
@IocBean
public class UserService {
    /**
     *
     */
    @Inject
    private UserDaoImpl userDaoImpl;
    /**
     *
     */
    private static Class<UserModel> CLASS = UserModel.class;

    public static final int USER_NOT_FOUND = 1;
    public static final int PASSWORD_NOT_MATCH = 2;
    public static final int REPEAT_ACCOUNT = 3;
    public static final int REPEAT_NAME = 4;
    public static final int REPEAT_IMEI = 5;
    public static final int REPEAT_UUID = 6;
    public static final int REPEAT_PWD_AG = 7;

    private LogicException error(int code) {
        switch (code) {
            case USER_NOT_FOUND:
                return new LogicException(code, "用户不存在");
            case PASSWORD_NOT_MATCH:
                return new LogicException(code, "密码错误");
            case REPEAT_ACCOUNT:
                return new LogicException(code, "账号重复");
            case REPEAT_NAME:
                return new LogicException(code, "用户名已存在");
            case REPEAT_IMEI:
                return new LogicException(code, "您还未注册");
            case REPEAT_UUID:
                return new LogicException(code, "您还未注册");
            case REPEAT_PWD_AG:
                return new LogicException(code, "两次输入不一致");
            default:
                return new LogicException(code, "未知错误");
        }
    }

    /**
     * @return
     * @throws LogicException
     */
    public UserModel login(String uName, String uPwd) throws LogicException {
        UserModel user =
                userDaoImpl.fetch(CLASS, Cnd.where("NAME", "=", uName).and("STATE", "<>", 0));
        if (user == null) {
            //用户不存在
            throw error(USER_NOT_FOUND);
        } else {
            //用户存在查询密码是否正确
            VerifyPwd(uPwd, user);
        }
        return user;
    }

    private void VerifyPwd(String testPass, UserModel user) throws LogicException {
        if (!CryptUtil.MD5(testPass).equals(user.getPass())) {
            throw error(PASSWORD_NOT_MATCH);
        }
    }

    public UserModel modfPwd(UserModel user, String npw, String oldpw, String npwa) throws LogicException {
        if (user != null) {
            switch (user.getPass().equals(CryptUtil.MD5(oldpw.trim())) ? 0 : 1) {
                case 0://密码正确
                    if (npw.equals(npwa)) {
                        user.setPass(CryptUtil.MD5(npwa));
                        Long uid = user.getId();
                        int modfState = userDaoImpl.modified(user);
                        if (modfState > 0) {
                            return userDaoImpl.fetch(CLASS, uid);
                        }
                    } else {
                        throw error(REPEAT_PWD_AG);
                    }
                case 1:
                    throw error(PASSWORD_NOT_MATCH);
            }
        }
        return null;
    }

    /**
     * @param type
     * @return
     * @throws Exception
     */
    public final List<MenuModel> getMenus(Integer type) throws Exception {
        List<MenuModel> models = null;
        if (type != null) {
            models = new ArrayList<MenuModel>();
            switch (type) {
                case 2:
                    models.add(new MenuModel("/org/doList.do", "机构管理", "../../../images/back/main/org_white.png", 1));
                    models.add(new MenuModel("/sect/toList.do", "码段管理", "../../../images/back/main/sec_white.png", 2));
                    models.add(new MenuModel("/data/toPage.do", "资料&说明", "../../../images/back/main/data_white.png", 4));
                    return models;
                case 3:
                    models.add(new MenuModel("/sect/toList.do", "证书管理", "../../../images/back/main/cer_white.png", 3));
                    models.add(new MenuModel("/data/toPage.do", "资料&说明", "../../../images/back/main/data_white.png", 4));
                    return models;
                case 4:
                    models.add(new MenuModel("/sect/toSecList.do", "阙音码管理", "../../../images/back/main/cer_white.png", 5));
                    models.add(new MenuModel("/content/toContentPage.do", "内容管理", "../../../images/back/main/data_white.png", 6));
                    return models;
                    default:
                    models.add(new MenuModel("/user/doLogin.do", "系统管理员", "", 0));
                    return models;
            }
        }
        return null;
    }

    public boolean elementStrIsNull(String elements[]) {
        for (String obj:elements){
            if (obj == null||obj.trim().equals("")){
                return false;
            }
        }
        return true;
    }
}
